package cloud.xlh.facade.file.impl;

import cloud.xlh.client.file.dto.FileHandleDTO;
import cloud.xlh.client.file.dto.MountSettingDTO;
import cloud.xlh.client.file.service.FileClientService;
import cloud.xlh.domain.file.domain.FileSaveDomain;
import cloud.xlh.domain.file.domain.MountSettingSaveDomain;
import cloud.xlh.domain.file.domain.MountSettingUpdateDomain;
import cloud.xlh.intergration.file.adapter.FileClientAdapter;
import cloud.xlh.service.file.service.FileService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;

/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : xlh_osms
 * @ Author: XuLeHuang
 * @ Date: 2023/8/5 15:11
 * @ Description:
 */
@Service
public class FileClientServiceImpl implements FileClientService {
    @Resource
    private FileClientAdapter fileClientAdapter;
    @Resource
    private FileService fileService;
    @Override
    public String fileHandle(FileHandleDTO fileHandleDTO) {
        String handleType = fileHandleDTO.getHandleType();
        if ("0".equals(handleType)) {
            FileSaveDomain  fileSaveDomain = fileClientAdapter.convertFileSaveDomain(fileHandleDTO);
            File file = new File(fileSaveDomain.getLocalPath());
            String name = file.getName();
            long totalSpace = file.getTotalSpace();
            fileSaveDomain.setName(name);
            fileSaveDomain.setSize(String.valueOf(totalSpace));
            return fileService.saveFile(fileSaveDomain);
        } else if ("1".equals(handleType)) {
            String remotePath = fileHandleDTO.getRemotePath();
            return fileService.deleteByRemotePath(remotePath);
        } else if ("2".equals(handleType)) {
            String localPath = fileHandleDTO.getLocalPath();
            String remotePath = fileHandleDTO.getRemotePath();
            return fileService.downFile(localPath , remotePath);
        } else {

        }
        return null;
    }

    @Override
    public String mountSetting(MountSettingDTO mountSettingDTO) {
        String settingType = mountSettingDTO.getSettingType();
        if ("0".equals(settingType)) {
            MountSettingSaveDomain mountSettingSaveDomain = fileClientAdapter.convertMountSettingSaveDomain(mountSettingDTO);
            return fileService.saveMountSetting(mountSettingSaveDomain);
        } else if ("1".equals(settingType)) {
            String mpLocalDirPath = mountSettingDTO.getMpLocalDirPath();
            String mpRemoteDirPath = mountSettingDTO.getMpRemoteDirPath();
            return fileService.deleteByMpPath(mpLocalDirPath , mpRemoteDirPath);
        } else if ("2".equals(settingType)) {
            MountSettingUpdateDomain mountSettingUpdateDomain = fileClientAdapter.convertMountSettingUpdateDomain(mountSettingDTO);
            return fileService.updateMountSetting(mountSettingUpdateDomain);
        } else {
        }
        return null;
    }
}
